import { getVideoList, videoDelete, videoDisable, videoInfo, videoSave } from '@/api/knowledge'
import { ref } from 'vue'

/**
 * 在线视频表单
 */
export const useVideoForm = () => {
  // 表单保存加载
  const formSaveLoading = ref(false)

  // 表单
  const form = ref<KnownAPIRequest.VideoForm>({
    delVideoPaths: [],
    id: '',
    image: '',
    name: '',
    status: false,
    videoGoodsRelationVos: [],
    videoPaths: [],
    visibilityType: 1,
  })

  /**
   * 表单保存
   */
  const formSave = async () => {
    formSaveLoading.value = true
    const res = await videoSave(form.value)
    formSaveLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    form.value = {
      delVideoPaths: [],
      id: '',
      image: '',
      name: '',
      status: false,
      videoGoodsRelationVos: [],
      videoPaths: [],
      visibilityType: 1,
    }
  }

  return {
    form,
    formSaveLoading,
    formSave,
    resetForm,
  }
}

/**
 * 获取视频列表
 */
export const useGetVideoList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 获取列表请求参数
  const getListParam = ref({
    limit: 50,
    name: '',
    page: 1,
  })

  // 视频列表
  const videoList = ref<Array<OnLineVideo>>([])

  // 列表总条数
  const listTotal = ref(0)

  /**
   * 获取视频列表
   */
  const getList = async () => {
    getListLoading.value = true
    const res = await getVideoList(getListParam.value)
    videoList.value = res.data.data.rows
    listTotal.value = Number(res.data.data.total)
    getListLoading.value = false
  }

  return {
    listTotal,
    videoList,
    getListParam,
    getListLoading,
    getList,
  }
}

/**
 * 批量删除视频
 */
export const useVideoDelByIds = () => {
  // 删除加载
  const delLoading = ref(false)

  /**
   * 批量删除
   * @param {Array<string>} ids - 视频id集合
   */
  const del = async (ids: Array<string>) => {
    delLoading.value = true
    const res = await videoDelete(ids)
    delLoading.value = false
    return res.data
  }

  return {
    delLoading,
    del,
  }
}

/**
 * 批量启用视频
 */
export const useVideoEnableByIds = () => {
  // 启用加载
  const enableLoading = ref(false)

  /**
   * 启用
   * @param {Array<string>} ids - 视频id集合
   */
  const enable = async (ids: Array<string>) => {
    enableLoading.value = true
    const res = await videoDisable(ids, false)
    enableLoading.value = false
    return res.data
  }

  return {
    enableLoading,
    enable,
  }
}

/**
 * 批量禁用视频
 */
export const useVideoDisableByIds = () => {
  // 禁用加载
  const disableLoading = ref(false)

  /**
   * 禁用
   * @param {Array<string>} ids - 视频id集合
   */
  const disable = async (ids: Array<string>) => {
    disableLoading.value = true
    const res = await videoDisable(ids, true)
    disableLoading.value = false
    return res.data
  }

  return {
    disableLoading,
    disable,
  }
}

/**
 * 获取视频信息
 */
export const useGetVideoInfo = () => {
  // 获取信息加载
  const getInfoLoading = ref(false)

  // 视频信息
  const info = ref<OnLineVideoInfo>({
    delVideoPaths: null,
    id: '',
    image: '',
    name: '',
    status: false,
    updateTime: '',
    videoGoodsRelationVos: [],
    videoPaths: [],
    visibilityType: 1,
  })

  /**
   * 获取信息
   * @param {String} id - 视频id
   */
  const getInfo = async (id: string) => {
    getInfoLoading.value = true
    const res = await videoInfo(id)
    info.value = res.data.data.row
    getInfoLoading.value = false
  }

  return {
    info,
    getInfoLoading,
    getInfo,
  }
}
